<template>
  <div class="comment">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <template #title>
          <div class="artInfo">
            <!-- 头像 -->
            <van-image
              fit="cover"
              round
              width="1.5rem"
              height="1.5rem"
              :src="item.aut_photo"
            />
            <!-- 评论者信息 -->
            <div class="info">
              <div class="name">{{ item.aut_name }}</div>
              <div class="content">{{ item.content }}</div>
              <div class="time">
                {{ item.pubdate
                }}<van-button
                  @click="showReply(item)"
                  plain
                  hairline
                  size="mini"
                  type="default"
                  >回复({{ item.reply_count }})</van-button
                >
              </div>
            </div>
            <!-- 爱心 -->
            <van-icon name="like-o" />
          </div>
        </template>
      </van-cell>
    </van-list>

    <!-- 输入内容 -->
    <div class="input-warp">
      <van-field
        v-model="cmtValue"
        @keyup.enter="send"
        center
        clearable
        placeholder="请输入评论内容"
      >
        <template #button>
          <van-button @click="send" size="small" type="info">发布</van-button>
        </template>
      </van-field>
    </div>

    <!-- 回复评论 -->
    <van-action-sheet v-model="sheetShow" title="发表回复">
      <reply v-if="sheetShow" :currentItem="currentItem" />
    </van-action-sheet>
  </div>
</template>

<script>
import { cmtListAPI, sendCmtAPI } from '@/api'
import reply from './reply'

export default {
  components: { reply },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      cmtValue: '',
      sheetShow: false,
      currentItem: {},
      offset: undefined
    }
  },
  methods: {
    // 回复面板点击事件
    showReply (item) {
      // 打开弹出框
      this.sheetShow = true
      // 将外面的值传给reply里面
      this.currentItem = item
    },
    async onLoad () {
      const res = await cmtListAPI({
        type: 'a',
        source: this.$route.query.id,
        offset: this.offset
      })
      // console.log(res)
      this.list.push(...res.data.data.results)
      // 把本次的最后一条id作为下次请求的参数
      this.offset = res.data.data.last_id
      this.loading = false

      //  当本次的最后一条id，等于 所有数据的结束id就意味着加载完了
      if (res.data.data.end_id === res.data.data.last_id) {
        this.finished = true
      }
    },

    // 发送评论
    async send () {
      // 非空判断
      if (this.cmtValue === '') {
        this.$toast('请输入评论内容')
        return
      }

      // 不为空，拿到数据发请求
      const res = await sendCmtAPI({
        // 文章id
        target: this.$route.query.id,
        content: this.cmtValue
      })
      // console.log(res)
      this.list.unshift(res.data.data.new_obj)
      // 清空评论框里的数据
      this.cmtValue = ''
      this.$toast('评论成功!')
    }
  }
}
</script>

<style lang="less">
.comment {
  .artInfo {
    display: flex;
    margin-top: 15px;
    .info {
      margin-left: 20px;
      flex: 1;
      .content {
        margin: 8px 0;
      }
      .name {
        color: royalblue;
      }
      .time {
        color: gray;
        height: 20px;
        display: flex;
        align-items: center;
      }
    }
  }
  .input-warp {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
  }
}
</style>
